using GameConfig.item;
using System;
using System.Collections;
using System.Collections.Generic;
using TEngine;
using UnityEngine;
namespace GameLogic
{
    public class ItemManager : SingletonBehaviour<ItemManager>
    {
        private Transform itemParent;


        private Transform PlayerTransform => FindObjectOfType<Player>().transform;

        //记录场景Item
        private Dictionary<string, List<SceneItem>> sceneItemDict = new Dictionary<string, List<SceneItem>>();
        //记录场景家具
        private Dictionary<string, List<SceneFurniture>> sceneFurnitureDict = new Dictionary<string, List<SceneFurniture>>();

        public void Init()
        {
            Debug.Log("ItemManager初始化了");
            GameEvent.AddEventListener<int, Vector3>(ILoginUI_Event.InstantiateItemInScene, OnInstantiateItemInScene);
        }

        private void OnEnable()
        {
            
            //EventHandler.InstantiateItemInScene += OnInstantiateItemInScene;
            //EventHandler.DropItemEvent += OnDropItemEvent;
            //EventHandler.BeforeSceneUnloadEvent += OnBeforeSceneUnloadEvent;
            //EventHandler.AfterSceneLoadedEvent += OnAfterSceneLoadedEvent;
            ////建造
            //EventHandler.BuildFurnitureEvent += OnBuildFurnitureEvent;
            //EventHandler.StartNewGameEvent += OnStartNewGameEvent;
        }

        private void OnDisable()
        {
            //GameEvent.RemoveEventListener<int, Vector3>(ILoginUI_Event.InstantiateItemInScene, OnInstantiateItemInScene);
            //EventHandler.InstantiateItemInScene -= OnInstantiateItemInScene;
            //EventHandler.DropItemEvent -= OnDropItemEvent;
            //EventHandler.BeforeSceneUnloadEvent -= OnBeforeSceneUnloadEvent;
            //EventHandler.AfterSceneLoadedEvent -= OnAfterSceneLoadedEvent;
            //EventHandler.BuildFurnitureEvent -= OnBuildFurnitureEvent;
            //EventHandler.StartNewGameEvent -= OnStartNewGameEvent;
        }

        private void Start()
        {
        }

        private void OnStartNewGameEvent(int obj)
        {
            sceneItemDict.Clear();
            sceneFurnitureDict.Clear();
        }
        private void OnBuildFurnitureEvent(int ID, Vector3 mousePos)
        {
            //BluePrintDetails bluePrint = InventoryManager.Instance.bluePrintData.GetBluePrintDetails(ID);
            //var buildItem = Instantiate(bluePrint.buildPrefab, mousePos, Quaternion.identity, itemParent);
            //if (buildItem.GetComponent<Box>())
            //{
            //    buildItem.GetComponent<Box>().index = InventoryManager.Instance.BoxDataAmount;
            //    buildItem.GetComponent<Box>().InitBox(buildItem.GetComponent<Box>().index);
            //}
        }

        private void OnBeforeSceneUnloadEvent()
        {
            GetAllSceneItems();
            GetAllSceneFurniture();
        }

        private void OnAfterSceneLoadedEvent()
        {
            itemParent = GameObject.FindWithTag("ItemParent").transform;
            RecreateAllItems();
            RebuildFurniture();
        }


        /// <summary>
        /// 在指定坐标位置生成物品
        /// </summary>
        /// <param name="ID">物品ID</param>
        /// <param name="pos">世界坐标</param>
        private void OnInstantiateItemInScene(int tbID, Vector3 vector)
        {
            Debug.Log("确实生成了这个场景物体");
            if (itemParent == null)
            {
                GameObject emptyGo = new GameObject("ItemParent");
                emptyGo.transform.position = Vector3.zero;
                emptyGo.AddComponent<DontDestory>();
                itemParent = emptyGo.transform;
            }
            var handle = PoolManager.Instance.GetGameObject("ItemBase", parent: itemParent);
            handle.gameObject.name = "拖拽创建的道具";
            handle.GetComponent<Item>().Init(tbID);
            handle.transform.position = vector;
            Debug.Log("这个场景物体的位置是X:"+handle.transform.position.x+"Y:"+ handle.transform.position.y+"Z:"+ handle.transform.position.z);
        }

        private void OnDropItemEvent(int ID, Vector3 mousePos, ItemType itemType)
        {
            //if (itemType == ItemType.Seed) return;

            //var item = Instantiate(bounceItemPrefab, PlayerTransform.position, Quaternion.identity, itemParent);
            //item.itemID = ID;
            //var dir = (mousePos - PlayerTransform.position).normalized;
            //item.GetComponent<ItemBounce>().InitBounceItem(mousePos, dir);
        }


        /// <summary>
        /// 获得当前场景所有Item
        /// </summary>
        private void GetAllSceneItems()
        {
            List<SceneItem> currentSceneItems = new List<SceneItem>();

            //foreach (var item in FindObjectsOfType<Item>())
            //{
            //    SceneItem sceneItem = new SceneItem
            //    {
            //        itemID = item.itemID,
            //        position = new SerializableVector3(item.transform.position)
            //    };

            //    currentSceneItems.Add(sceneItem);
            //}
            //foreach (var item in FindObjectsOfType<Box>())
            //{
            //    if (item.GetComponent<Furniture>() == null)
            //    {
            //        item.GetComponent<Box>().InitBox(item.GetComponent<Box>().index);
            //    }
            //}
            //if (sceneItemDict.ContainsKey(SceneManager.GetActiveScene().name))
            //{
            //    //找到数据就更新item数据列表
            //    sceneItemDict[SceneManager.GetActiveScene().name] = currentSceneItems;
            //}
            //else    //如果是新场景
            //{
            //    sceneItemDict.Add(SceneManager.GetActiveScene().name, currentSceneItems);
            //}
        }


        /// <summary>
        /// 刷新重建当前场景物品
        /// </summary>
        private void RecreateAllItems()
        {
            //List<SceneItem> currentSceneItems = new List<SceneItem>();

            //if (sceneItemDict.TryGetValue(SceneManager.GetActiveScene().name, out currentSceneItems))
            //{
            //    if (currentSceneItems != null)
            //    {
            //        //清场
            //        foreach (var item in FindObjectsOfType<Item>())
            //        {
            //            Destroy(item.gameObject);
            //        }

            //        foreach (var item in currentSceneItems)
            //        {
            //            Item newItem = Instantiate(itemPrefab, item.position.ToVector3(), Quaternion.identity, itemParent);
            //            newItem.Init(item.itemID);
            //        }
            //    }
            //}
        }


        /// <summary>
        /// 获得场景所有家具
        /// </summary>
        private void GetAllSceneFurniture()
        {
            //List<SceneFurniture> currentSceneFurniture = new List<SceneFurniture>();

            //foreach (var item in FindObjectsOfType<Furniture>())
            //{
            //    SceneFurniture sceneFurniture = new SceneFurniture
            //    {
            //        itemID = item.itemID,
            //        position = new SerializableVector3(item.transform.position)
            //    };
            //    if (item.GetComponent<Box>())
            //        sceneFurniture.boxIndex = item.GetComponent<Box>().index;

            //    currentSceneFurniture.Add(sceneFurniture);
            //}

            //if (sceneFurnitureDict.ContainsKey(SceneManager.GetActiveScene().name))
            //{
            //    //找到数据就更新item数据列表
            //    sceneFurnitureDict[SceneManager.GetActiveScene().name] = currentSceneFurniture;
            //}
            //else    //如果是新场景
            //{
            //    sceneFurnitureDict.Add(SceneManager.GetActiveScene().name, currentSceneFurniture);
            //}
        }

        /// <summary>
        /// 重建当前场景家具
        /// </summary>
        private void RebuildFurniture()
        {
            List<SceneFurniture> currentSceneFurniture = new List<SceneFurniture>();

            //if (sceneFurnitureDict.TryGetValue(SceneManager.GetActiveScene().name, out currentSceneFurniture))
            //{
            //    if (currentSceneFurniture != null)
            //    {
            //        foreach (SceneFurniture sceneFurniture in currentSceneFurniture)
            //        {
            //            BluePrintDetails bluePrint = InventoryManager.Instance.bluePrintData.GetBluePrintDetails(sceneFurniture.itemID);
            //            var buildItem = Instantiate(bluePrint.buildPrefab, sceneFurniture.position.ToVector3(), Quaternion.identity, itemParent);
            //            if (buildItem.GetComponent<Box>())
            //            {
            //                buildItem.GetComponent<Box>().InitBox(sceneFurniture.boxIndex);
            //            }
            //        }
            //    }
            //}
            //foreach (var item in FindObjectsOfType<Box>())
            //{
            //    item.GetComponent<Box>().InitBox(item.GetComponent<Box>().index);
            //}
        }

        //public GameSaveData GenerateSaveData()
        //{
        //    GetAllSceneItems();
        //    GetAllSceneFurniture();

        //    GameSaveData saveData = new GameSaveData();
        //    saveData.sceneItemDict = this.sceneItemDict;
        //    saveData.sceneFurnitureDict = this.sceneFurnitureDict;

        //    return saveData;
        //}

        //public void RestoreData(GameSaveData saveData)
        //{
        //    this.sceneItemDict = saveData.sceneItemDict;
        //    this.sceneFurnitureDict = saveData.sceneFurnitureDict;

        //    RecreateAllItems();
        //    RebuildFurniture();
        //}
    }
}